package ${packageName}.common.base.rest;


import ${packageName}.common.base.service.IBaseService;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;

/**
 * @author longxin
 *
 * @param <DOMAIN>
 * @param <ID>
 * @param <S>
 */
@Slf4j
public class IBaseRest<DOMAIN, ID,  S extends IBaseService<DOMAIN, ID>, REST extends IBaseRest> {


    /**
     * 动态 IOC代理
     *
     * 多个需要使用Autowired
     */
    @Autowired
    protected S service;

    /**
     * 当前泛Controller真实类型的Class
     */
    protected Class<REST> restClass;

    public IBaseRest() {
        ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
        //pt包含BaseController<DOMAIN, ID,  S extends IBaseService<DOMAIN, ID>, Control>中的各个参数类
        restClass = (Class<REST>) pt.getActualTypeArguments()[3];
    }



    /**
     * 根据ID获取
     * @param id
     * @return
     */
    @ApiOperation(value = "#根据ID获取")
    @RequestMapping(value = "superGet/{id}", method = RequestMethod.GET)
    public R<?> superGetByIdNoFilter(@PathVariable("id") ID id) {
        log.info("=========>>>#根据ID获取");
        return R.ok(service.getById((Serializable) id));
    }

    /**
     * 新增/更新数据
     * @param entity
     * @return
     */
    @ApiOperation(value = "#新增/更新")
    @RequestMapping(value = "superEdit", method = RequestMethod.PUT)
    public R<?> superEdit(DOMAIN entity) {
        log.info("=========>>>#新增/更新");
        return R.ok(service.saveOrUpdate(entity));
    }

    /**
     * 分页列表
     * @param page
     * @return
     */
    @ApiOperation(value = "#分页列表")
    @RequestMapping(value = "superList", method = {RequestMethod.GET})
    public R<?> superPageList(Page page) {
        log.info("=========>>>#分页列表");
        return R.ok(service.page(page));
    }
}
